<?php
class Data_Model_ChiTietKhoaHocMapper extends \Data_Model_DataMapperAbstract {
	
		protected static $_instance = null;
		// dung de phan trang
		private $_paginator = null;
	
		public function searchByMahocvien($id)
		{
			$db = $this->getDb();
			$selectCtkh = $db->select()
							->from('tbl_chitietkhoahoc')
							->where('MaHocVien = ?',$id);
			$ctkhs = $db->fetchAll($selectCtkh);
		
			$ctkhArray = array();
			foreach($ctkhs as $ctkh)
			{
				
				
				$hocvienMapper = Data_Model_HocVienMapper::getInstance();
				$hocvien = $hocvienMapper->searchById($id);
				$ctkh['tbl_hocvien'] = $hocvien;
				
				$khoahocMapper = Data_Model_KhoaHocMapper::getInstance();
				$khoahoc = $khoahocMapper->searchByMakhoahoc($ctkh['MaKhoaHoc']);
				$ctkh['tbl_khoahoc'] = $khoahoc;
					
				$ctkhArray[] = $this->_populate($ctkh);
			}
			return $ctkhArray;
		}
		public function searchByKh($id)
		{
			$db = $this->getDb();
			$ctkhSelect = $db->select()
					   ->from('tbl_chitietkhoahoc')
					   ->join('tbl_khoahoc','tbl_khoahoc.MaKhoaHoc = tbl_chitietkhoahoc.MaKhoaHoc')
					   ->where('tbl_chitietkhoahoc.MaKhoaHoc = ?',$id);
			$ctkhs = $db->fetchAll($ctkhSelect);
			$ctkhArray = array();
			foreach($ctkhs as $ctkh)
			{
				$hocvienMapper = Data_Model_HocVienMapper::getInstance();
				$hocvien = $hocvienMapper->searchById($ctkh['MaHocVien']);
				$ctkh['tbl_hocvien'] = $hocvien;
				
				$ctkhArray[] =  $this->_populate($ctkh);				
			}
			return $ctkhArray;
		}
	/* (non-PHPdoc)
	 * @see Data_Model_DataMapperAbstract::_insert()
	 */protected function _insert(Data_Model_DomainObjectAbstract $obj) {
		// TODO Auto-generated method stub

			$db = $this->getDb();
			/* $madangky = $db->lastInsertId('tbl_dangkykhoahoc','MaDangKy'); */
			$data = array(
					'MaKhoaHoc' => $obj->getMakhoahoc(),
					'MaHocVien' => $obj->getMahocvien(),
					'BuoiHoc'   => $obj->getBuoiHoc(),
					'CaHoc'		=> $obj->getCahoc()
			);
			$db->insert('tbl_chitietkhoahoc',$data);
				
		}

	/* (non-PHPdoc)
	 * @see Data_Model_DataMapperAbstract::_update()
	 */protected function _update(Data_Model_DomainObjectAbstract $obj) {
		// TODO Auto-generated method stub
		$db = $this->getDb();
		$data = array(
				'MaKhoaHoc' => $obj->getMakhoahoc(),
				'MaHocVien' => $obj->getMahocvien(),
				'BuoiHoc'   => $obj->getBuoiHoc(),
				'CaHoc'		=> $obj->getCahoc()
		);
		$where = $db->quoteInto('ID = ?',$obj->getId());
		$db->update('tbl_chitietkhoahoc',$data,$where);
		}

	/* (non-PHPdoc)
	 * @see Data_Model_DataMapperAbstract::_populate()
	 */protected function _populate($data) {
		// TODO Auto-generated method stub
		$obj = new Data_Model_ChiTietKhoaHoc();
		
		$obj->setId($data['ID']);
		$obj->setMakhoahoc($data['MaKhoaHoc']);
		$obj->setMahocvien($data['MaHocVien']);
		$obj->setBuoihoc($data['BuoiHoc']);
		$obj->setCahoc($data['CaHoc']);
		$obj->setHocvien($data['tbl_hocvien']);
		$obj->setKhoahoc($data['tbl_khoahoc']);
		
		return $obj;
		}
		
		public function delete($id)
		{
			$db = $this->getDb();
			$where = $db->quoteInto('ID = ?',$id);
			$db->delete('tbl_chitietkhoahoc',$where);
		}
		
		// Goi chinh no
		
		public static function getInstance()
		{
			if(null === self::$_instance)
			{
				self::$_instance = new self();
			}
			return self::$_instance;
		}
}

?>